package com.blog.wu.myblog.activemq;

import com.blog.wu.myblog.base.BaseService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @Author wuyanshen
 * @Date 2018-12-25 4:30 PM
 * @Discription springboot整合后的 消费者
 */
@Component
public class MQConsumer extends BaseService {

    @JmsListener(destination = "${queue}")
    public void receive(TextMessage textMessage, Session session) {
        String message = null;
        try {
            message = textMessage.getText();

            /**全局消息id，每条消息的id都是唯一的 ，用全局id可以避免重复消费*/
            String id = textMessage.getJMSMessageID();

            //手动进行签收
            textMessage.acknowledge();
        } catch (Exception e) {

            //在catche
            try {
                session.recover();//重试
            } catch (JMSException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        logger.info("我是消费者，我消费了："+message);
    }
}
